﻿using System;
using System.IO;
using System.Reflection;
using System.Text;

namespace UFrame
{
    /// <summary>
    /// 分析text文本每一行存入对应字段里面去
    /// </summary>
    public static class RefLineToObjectTool
    {
        //public static char fuhao = ':';

        /// <summary>
        /// 通过反射读取文本， 把值赋值到类字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="text"></param>
        /// <returns></returns>
        public static T AnalyzeTxt<T>(string text, char fuhao = ':') where T : new()
        {
            T obj = new T();
            Type t = typeof(T);
            FieldInfo[] fileInfo = t.GetFields(BindingFlags.Instance | BindingFlags.Public);
            string[] lines = text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < lines.Length; i++)
            {
                string[] keyPar = lines[i].Split(fuhao);
                for (int z = 0; z < fileInfo.Length; z++)
                {
                    if (keyPar[0].Equals(fileInfo[z].Name, StringComparison.OrdinalIgnoreCase))
                    {
                        FieldInfo info = fileInfo[z];
                        string fieldType = info.FieldType.Name;
                        string fieldName = info.Name;
                        string fieldValue = keyPar[1];

                        if (fieldType == "Float" || fieldType == "Single")
                        {
                            fileInfo[z].SetValue(obj, Convert.ToSingle(fieldValue));
                        }

                        if (fieldType == "Int32" || fieldType == "Int" || fieldType == "Int16")
                        {
                            fileInfo[z].SetValue(obj, Convert.ToInt32(fieldValue));
                        }

                        if (fieldType == "Double")
                        {
                            fileInfo[z].SetValue(obj, Convert.ToDouble(fieldValue));
                        }

                        if (fieldType == "String" || fieldType == "string")
                        {
                            fileInfo[z].SetValue(obj, fieldValue);
                        }
                    }
                }
            }

            return obj;
        }

        public static void WriteAnalyzeTxt<T>(string path, T t, char fuhao = ':') where T : new()
        {
            var type = t.GetType();
            FieldInfo[] fileInfo = type.GetFields(BindingFlags.Instance | BindingFlags.Public);

            StringBuilder sb = new StringBuilder();
            foreach (var info in fileInfo)
            {
                string val = info.GetValue(t).ToString();
                string name = info.Name;
                sb.AppendLine(name + "=" + val);
            }

            string text = sb.ToString();
            if (File.Exists(path))
            {
                File.WriteAllText(path, text, Encoding.UTF8);
            }
            else
            {
                var dir = Path.GetDirectoryName(path);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                FileStream f = File.Create(path);
                //FileStream f = new FileStream(path, FileMode.OpenOrCreate);
                byte[] add = Encoding.UTF8.GetBytes(text);
                f.Write(add, 0, add.Length);
                f.Close();
            }
        }
    }
}